﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Client
{

    public partial class Login : Form
    {
        MySqlConnection connection;
        MySqlDataReader reader;
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("密码不能为空");
            }
            if (textBox1.Text != "" || textBox2.Text != "")
            {
                string querry = "select username from users where username = '" + textBox1.Text + "'and pwd = '" + textBox2.Text + "'";
                if (this.OpenConn() == true)
                {
                    MySqlCommand cmd = new MySqlCommand(querry, connection);
                    reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        this.Hide();
                        MessageBox.Show("登录成功");
                        Client client = new Client();
                        client.Show();
                        this.CloseConn();
                    }
                    else
                    {
                        MessageBox.Show("登录失败");
                    }
                }
            }
        }
        private bool OpenConn()
        {
            try
            {
                string str = "server=localhost;user=root;password=@-Sq12194;database=userlist;";
                connection = new MySqlConnection(str);
                connection.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }
        private bool CloseConn()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空");
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("密码不能为空");
            }
            try
            {
                if (textBox1.Text != "" || textBox2.Text != "")
                {
                    string querry = "insert into users set username = '" + textBox1.Text + "',pwd = '" + textBox2.Text + "';";
                    if (this.OpenConn() == true)
                    {
                        MySqlCommand cmd = new MySqlCommand(querry, connection);
                        if (cmd.ExecuteNonQuery() != 0)
                        {
                            MessageBox.Show("注册成功");
                        }
                    }

                }

            }
            catch
            {
                MessageBox.Show("注册失败");
            }
        }
    }
}
